/**
 *  //yangwei
 *  *2010-6-24 下午10:08:05  add 3
 */
package com.base.myproject.client.HR.bcode;


import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;

/**
 * HR查找人员的通用对话框  add 4
 * @author Administrator
 *2010-6-24
 * javazgw@gmail.com
 */
public class QueryBcodeDialog {
	
	Dialog d = new Dialog();
	BaseStore<BusiObject> store;
	DateTimeFormat activeFormat = DateTimeFormat
	.getFormat("yyyy-MM-dd H:mm:ss");
	ContentPanel cp = new ContentPanel();
	public QueryBcodeDialog()
	{
		init();
	}

	private void init() {
	
		initWidget();
		d.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.gj_find1()));
		//gj_find1
	}
	BaseComboBoxX bc16;
	BaseComboBoxX C_num17;
	BaseComboBoxX C_num18 ;
	BaseComboBoxX C_num5 ;
	BaseComboBoxX C_num6;
	BaseComboBoxX C_num20;
	
	BaseComboBoxX C_num21;
	BaseComboBoxX C_num22;
	
	
	DateField C_data9 ;
	DateField C_data10;
	
	
	private void initWidget() {
		TableLayout tl = new TableLayout();
		tl.setColumns(4);
		cp.setLayout(tl);
		
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(5);
		
		
		
		TextField<String> name = new TextField<String>();
		
		 bc16 = new BaseComboBoxX("name","dictionary", "where type='hr_p'");
		 C_num17 = new BaseComboBoxX("name","dictionary", "where type='hr_c'");
		 C_num18 = new BaseComboBoxX("name","dictionary", "where type='hr_a'");
		
		
		
		cp.add(new Label("姓名:"),td);
		cp.add(name,td);
		cp.add(new Label("户籍省份:"),td);
		cp.add(bc16,td);
		cp.add(new Label("户籍市:"),td);
		cp.add(C_num17,td);
		cp.add(new Label("户籍区:"),td);
		cp.add(C_num18,td);
		
		cp.add(new Label("雇佣级别:"), td);
		 C_num5 = new BaseComboBoxX("name","dictionary", "where type='hr_e_level'");
		cp.add(C_num5,td);
		
		
		cp.add(new Label("内部职位:"), td);
		 C_num6 = new BaseComboBoxX("name","dictionary", "where type='hr_title'");
		cp.add(C_num6,td);
		
		
		cp.add(new Label("工作类别:"), td);
		C_num20 = new BaseComboBoxX("name","dictionary", "where type='hr_jobtype'");
		cp.add(C_num20,td);
		
		
		cp.add(new Label("入职日期 从:"), td);
		 C_data9 = new DateField();
		cp.add(C_data9,td);
		
		cp.add(new Label("到:"), td);
		 C_data10 = new DateField();
		cp.add(C_data10,td);
		
		cp.add(new Label("工作性质:"), td);
		C_num21 = new BaseComboBoxX("name","dictionary", "where type='hr_jobproperty'");
		cp.add(C_num21,td);
		
		cp.add(new Label("工作组别:"), td);
		C_num22 = new BaseComboBoxX("name","dictionary", "where type='hr_jobgroup'");
		cp.add(C_num22,td);
		
		cp.setHeaderVisible(false);
		
	}
	/**
	 * 加入外部查询事件则屏蔽内部的查询事件
	 * @param listener
	 */
	public void addQueryButton(Listener<BaseEvent> listener)
	{
		d.getButtonById(Dialog.OK).addListener(Events.Select, listener);
		
		
	}
	/**
	 * 显示对话框，如果查询按钮没有事件则使用默认事件
	 */
	
	public void show()
	{
		d.add(cp);
		d.setButtons(Dialog.OKCANCEL);
		d.getButtonById(Dialog.OK).setText("查询");
		//if(d.getButtonById(Dialog.OK).getListeners(Events.Select).size()<=0)
		d.getButtonById(Dialog.OK).addListener(Events.Select, getListener());
		//3
		d.getButtonById(Dialog.CANCEL).addListener(Events.Select, getListener());
			
		d.setSize(529, 297);//设置弹出层的宽高
		  
	    d.show();
	}

	public BaseStore<BusiObject> getStore() {
		return store;
	}

	public void setStore(BaseStore<BusiObject> store) {
		this.store = store;
	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							 if (be.getSource().equals(d.getButtonById(Dialog.OK))) {
						
								
								querysql();
							 }
							 else if (be.getSource().equals(d.getButtonById(Dialog.CANCEL))) {
								//2
								 close();
									
									
								 }
						}
					}
				}
				finally
				{
					sync--;
				}
			}
		};
		
		return defaultlistener;
	}
	//1
public void close(){
	d.setVisible(false);
}
	void querysql()
	{
		 String sql = "select  * from bcode where 1=1 ";
		 //省
		 if(bc16.getValue()!=null && bc16.getValue().get("value")!=null)
		 {
			 sql+=" and hr_Census_Province='"+bc16.getValue().get("value")+"' ";
		 }
		 //市
		 if(C_num17.getValue()!=null && C_num17.getValue().get("value")!=null)
		 {
			 sql+=" and hr_Census_city='"+C_num17.getValue().get("value")+"' ";
		 }
		 //区
		 if(C_num18.getValue()!=null && C_num18.getValue().get("value")!=null)
		 {
			 sql+=" and hr_Census_area='"+C_num18.getValue().get("value")+"' ";
		 }
		 //雇佣级别
		 if(C_num5.getValue()!=null && C_num5.getValue().get("value")!=null)
		 {
			 //sql+="  and employlevel='"+C_num5.getValue().get("value")+"' ";
			 sql+= "  and exists (select * from bcode_job where employlevel ='"+C_num5.getValue().get("value")+"' and bcode_job.bcode=bcode.bcode)";
		 }
		 //内部职位
		 if(C_num6.getValue()!=null && C_num6.getValue().get("value")!=null)
		 {
			// sql+=" and employinnertitle='"+C_num6.getValue().get("value")+"' ";
			 sql+= "  and exists (select * from bcode_job where employinnertitle ='"+C_num6.getValue().get("value")+"' and bcode_job.bcode=bcode.bcode)";
		 }
		 //工作类别
		 if(C_num20.getValue()!=null && C_num20.getValue().get("value")!=null)
		 {
			// sql+=" and employoutertitle='"+C_num20.getValue().get("value")+"' ";
			 sql+= "  and exists (select * from bcode_job where jobtype ='"+C_num20.getValue().get("value")+"' and bcode_job.bcode=bcode.bcode)";
		 }
		//工作性质
		 if(C_num21.getValue()!=null && C_num21.getValue().get("value")!=null)
		 {
			 sql+= "  and exists (select * from bcode_job where jobproperty ='"+C_num21.getValue().get("value")+"' and bcode_job.bcode=bcode.bcode)";
		 }
		//工作组别
		 if(C_num22.getValue()!=null && C_num22.getValue().get("value")!=null)
		 {
			 sql+= "  and exists (select * from bcode_job where jobgroup ='"+C_num22.getValue().get("value")+"' and bcode_job.bcode=bcode.bcode)";
		 }
		 //入职日期 从
		 if(C_data9.getValue()!=null )
		 {
			 sql+=" and hr_entry_date>'"+activeFormat.format(C_data9.getValue())+"' ";
		 }
		 //入职日期 到
		 if(C_data10.getValue()!=null )
		 {
			 sql+=" and hr_entry_date<'"+activeFormat.format(C_data10.getValue())+"' ";
		 }
		 
			store.getStoreinfo().setSql(sql);
			store.getStoreinfo().setForcenolimit("true");
//			store.getStoreinfo().setNeedAllRow("false");
			store.reload();
	}

}
